package top.hepingan.java.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.hepingan.java.entity.*;
import top.hepingan.java.mapper.ShopImageMapper;
import top.hepingan.java.mapper.ShopMapper;
import top.hepingan.java.mapper.ShopStarMapper;
import top.hepingan.java.mapper.UserMapper;
import top.hepingan.java.service.ShopService;
import top.hepingan.java.utils.BeanUtils;
import top.hepingan.java.utils.UserUtils;
import top.hepingan.java.vo.ShopVo;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    private final ShopMapper shopMapper;
    private final ShopImageMapper shopImageMapper;
    private final UserMapper userMapper;


    /**
     * 分页查询商品
     * @param pageNum
     * @param category
     * @param userId
     * @return
     */
    @Override
    public List<ShopVo> pageBynum(Integer pageNum, String shopName, Long userId,String category,Boolean isTotal,Boolean isAllow) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        if (shopName != null && !shopName.equals("")){
            queryWrapper.lambda().like(Shop::getShopName, shopName);
        }
        if (userId != null ){
            queryWrapper.lambda().eq(Shop::getUserId, userId);
        }
        if (category != null && !category.equals("")){
            queryWrapper.lambda().eq(Shop::getCategory, category);
        }
        if (!isTotal){
            queryWrapper.lambda().eq(Shop::getIsOnline, true);
        }
        if (isAllow){
            queryWrapper.lambda().eq(Shop::getStatus, 1);
        }
        int pageSize = 10;

        IPage<Shop> page = shopMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper.lambda()
                .orderByDesc(Shop::getCreatedTime));
        List<ShopVo> shopVoList = new ArrayList<>();
        for (Shop shop : page.getRecords()){
            List<ShopImage> shopImageList = shopImageMapper.selectList(new QueryWrapper<ShopImage>().lambda().eq(ShopImage::getShopId, shop.getId()));
            List<String> imageList = new ArrayList<>();
            for (ShopImage shopImage : shopImageList){
                imageList.add(shopImage.getImageUrl());
            }
            ShopVo shopVo = BeanUtils.copyClassProperTies(shop, ShopVo.class);
            User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getId, shop.getUserId()));
            shopVo.setUsername(user.getUsername());
            shopVo.setLevel(user.getLevel());
            shopVo.setAvatar(user.getAvatar());
            shopVo.setAvatarFrameUrl(user.getAvatarFrameUrl());
            shopVo.setImageUrl(imageList);
            shopVoList.add(shopVo);
        }
        return shopVoList;
    }

    /**
     * 发布商品
     * @param shopVo
     */
    @Override
    public void publish(ShopVo shopVo) {
        Shop shop = BeanUtils.copyClassProperTies(shopVo, Shop.class);
        shop.setCreatedTime(LocalDateTime.now());
        shop.setUserId(UserUtils.getCurrentLoginId());
        shop.setIsOnline(true);
        shop.setStatus(0);
        shopMapper.insert(shop);
        for (String url : shopVo.getImageUrl()){
            ShopImage shopImage = new ShopImage();
            shopImage.setShopId(shop.getId());
            shopImage.setImageUrl(url);
            shopImageMapper.insert(shopImage);
        }
    }



}
